<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 等级关系图
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Levelview extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
     
        return $this->view->fetch();
    }
    
    //关系数据
    public function getData()
    {
        $mobile = $this->request->param('mobile', '');
        if (!empty($mobile)) {
            $user = Db::name('user')->where(['mobile' => $mobile])->find();
            $pid = (empty($user) ? 0 : $user['id']);
            $InviteCode = empty($user)?'':$user['unique_id'];
            $data = [
                'name'=>($user['nickname']==''?'【空】':$user['nickname'])."【{$user['unique_id']}】\r\n[{$user['mobile']}]\r\n推荐人：【{$InviteCode}】\r\n ID：【{$user['id']}】",
                'mobile'=>$user['mobile'],
                'unique_id'=>$user['unique_id'],
                'parent_invite_code'=>$InviteCode,
                "children"=>$this->getChild($user['id'],$user['unique_id'],4)
            ];
        } else {
            $pid = 0;
            $InviteCode = '';
            $data = [
                'name'=>'【空】'."【空】\r\n[空]\r\n推荐人：【空】",
                'mobile'=>'',
                'unique_id'=>'',
                'parent_invite_code'=>$InviteCode,
                "children"=>$this->getChild($pid, $InviteCode, 4)
            ];
        }
        $this->success('', '', $data);
    }

        /*
         * $userId 用户id
         * $InviteCode 用户邀请码
         * $level 往下查几级
         */
        private function getChild($userId,$InviteCode,$level) {
            if($level<=0){
                return [];
            }
        $userList = Db::name('user')->where(['parent_ids'=>['like',"%,{$userId},"]])->select();
        if(empty($userList)){
            return [];
        }
        $items = [];
        foreach ($userList as $user) {
            $items[] = [
                'name'=>($user['nickname']==''?'【空】':$user['nickname'])."【{$user['unique_id']}】\r\n[{$user['mobile']}]\r\n推荐人：【{$InviteCode}】\r\n ID：【{$user['id']}】",
                'mobile'=>$user['mobile'],
                'unique_id'=>$user['unique_id'],
                'parent_invite_code'=>$InviteCode,
                "children"=>$this->getChild($user['id'],$user['unique_id'],$level-1)
            ];
        }
        return $items;


    }
    
    private function getChild_bak($pid,$level,$endLevel) {
        $level =$level+1;

        $info = Db::name("expert_position")
                ->alias('p')
                ->field('p.*,u.nickname,u.mobile,u.avatar,u.unique_id,pu.unique_id as p_id')
                ->join('user u ','p.user_id= u.id')
                ->join('user pu ','u.parent_user_id= pu.id',"LEFT")
                ->where(['p.user_id'=>$pid])
                ->find();
        if(empty($info)){
            return [
                'name'=>""
            ];
        }
        $childIds = [];
        for($i=1;$i<=$info['limit_position_num'];$i++){
            if(isset( $info["p{$i}"])&&!empty($info["p{$i}"])){
                $childIds[] = $info["p{$i}"];
            }
        }
        $childList = Db::name("user")
                 ->alias('u')
                ->field('u.id,u.nickname,u.mobile,u.avatar,u.unique_id, pu.unique_id as p_id')
                ->join('user pu ','u.parent_user_id= pu.id',"LEFT")
                ->whereIn('u.id',$childIds)
                ->column('u.id,u.nickname,u.mobile,u.avatar,u.unique_id, pu.unique_id as p_id','u.id');
//        var_dump($childList);die;
        $childList["0"] =[
            "nickname"=>'空',
            "mobile"=>'',
            "unique_id"=>'',
            "avatar"=> letter_avatar('空'),
            "id"=>'0',
            "p_id"=>'0',
        ];
        $data =[];
        $data['name']= $info['nickname']!=''?$info['nickname'] ."【{$info['unique_id']}】"."\r\n[{$info['mobile']}]\r\n直推人：【{$info['p_id']}】":'空'."【{$info['unique_id']}】"."\r\n[{$info['mobile']}]\r\n直推人【{$info['p_id']}】";
        $data['name']= '我'."【{$info['unique_id']}】{$info['user_id']}"."\r\n[{$info['mobile']}]\r\n直推人【{$info['p_id']}】";
        $data['mobile']= $info['mobile']!=''?$info['mobile']:'空';
        $data['avatar']= $info['avatar']!=''?$info['avatar']:'';
        $data['user_id']= $info['user_id']!=''?$info['user_id']:'';

        for($i=1;$i<=$info['limit_position_num'];$i++){
            $id = $info["p{$i}"];
             if($id!=0){
                 $tmpUser =$childList[$id];
                $tmp =[
                    "name"=>$tmpUser['nickname']."【{$tmpUser['unique_id']}】\r\n[{$tmpUser['mobile']}]\r\n推荐人：【{$tmpUser['p_id']}】",
                    "mobile"=>$tmpUser['mobile'],
                    "avatar"=>$tmpUser['avatar'],
                    "user_id"=>$tmpUser['id']

                ];
             }else{
            $tmp =[
                "name"=>$childList["{$id}"]['nickname'],
                "mobile"=>$childList["{$id}"]['mobile'],
                "avatar"=>$childList["{$id}"]['avatar'],
                "user_id"=>$childList["{$id}"]['id']
               
            ];
             }
            if($id==0){
                $tmp["name"] = $childList["{$id}"]['nickname'];
            }else{
                $position= Db::name("expert_position") ->where(['user_id'=>$id])->find();
               if(!empty($position) && $level<=$endLevel){
                   $tmp = $this->getChild($position['user_id'], $level, $endLevel);
               }
            }

                
            $data['children'][]= $tmp;
                
            
         }
         return $data;
        
    }
    

}
